package com.qiwenshare.ufop.provider.util;

import com.qiwenshare.ufop.constant.UFOPConstants;
import com.qiwenshare.ufop.exception.UFOPException;
import jakarta.validation.constraints.NotNull;
import org.apache.commons.lang3.StringUtils;
import org.springframework.util.ResourceUtils;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * UFOPUtils
 */
public class UFOPUtils {

    public static final String[] TXT_FILE = {};
    public static String LOCAL_STORAGE_PATH;
    public static String ROOT_PATH;

    private UFOPUtils() {
        throw new UFOPException("该类不能被实例化");
    }

    public static String pathSplitFormat(String filePath) {
        return filePath.replace("///", "/")
                .replace("//", "/")
                .replace("\\\\\\", "/")
                .replace("\\\\", "/");
    }

    public static File getLocalSaveFile(String fileUrl) {
        String localSavePath = UFOPUtils.getDataPath() + fileUrl;
        return new File(localSavePath);
    }

    public static File getCacheFile(String fileUrl) {
        String cachePath = UFOPUtils.getDataPath() + "cache" + File.separator + fileUrl;

        return new File(cachePath);
    }

    public static File getTempFile(String fileUrl) {
        String tempPath = UFOPUtils.getDataPath() + "temp" + File.separator + fileUrl;
        File tempFile = new File(tempPath);
        File parentFile = tempFile.getParentFile();
        if (!parentFile.exists()) {
            parentFile.mkdirs();
        }

        return tempFile;
    }

    public static File getProcessFile(String fileUrl) {
        String processPath = UFOPUtils.getDataPath() + "temp" + File.separator + "process" + File.separator + fileUrl;
        File processFile = new File(processPath);
        File parentFile = processFile.getParentFile();
        if (!parentFile.exists()) {
            parentFile.mkdirs();
        }
        return processFile;
    }

    /**
     * 获取项目所在的根目录路径 resources路径
     *
     * @return 结果
     */
    public static String getProjectRootPath() {
        String absolutePath;
        try {
            String url = ResourceUtils.getURL("classpath:").getPath();
            absolutePath = urlDecode(new File(url).getAbsolutePath()) + File.separator;
        } catch (FileNotFoundException e) {
            throw new UFOPException(e);
        }

        return absolutePath;
    }

    /**
     * 路径解码
     *
     * @param url url
     * @return 结果
     */
    public static String urlDecode(String url) {
        String decodeUrl;
        try {
            decodeUrl = URLDecoder.decode(url, "utf-8");
        } catch (UnsupportedEncodingException e) {
            throw new UFOPException("不支持的编码格式", e);
        }
        return decodeUrl;
    }


    public static String getDataPath() {
        String localStoragePath = LOCAL_STORAGE_PATH;
        if (StringUtils.isNotEmpty(localStoragePath)) {

            return new File(localStoragePath).getPath() + File.separator;
        } else {
            String projectRootAbsolutePath = getProjectRootPath();

            int index = projectRootAbsolutePath.indexOf(UFOPConstants.FILE + UFOPConstants.COLON_IDENTIFIER);
            if (index != -1) {
                projectRootAbsolutePath = projectRootAbsolutePath.substring(0, index);
            }

            return new File(projectRootAbsolutePath + "data").getPath() + File.separator;
        }
    }

    /**
     * 得到static路径
     *
     * @return 结果
     */
    public static String getStaticPath() {

        String projectRootAbsolutePath = getProjectRootPath();

        int index = projectRootAbsolutePath.indexOf(UFOPConstants.FILE + UFOPConstants.COLON_IDENTIFIER);
        if (index != -1) {
            projectRootAbsolutePath = projectRootAbsolutePath.substring(0, index);
        }

        return new File(projectRootAbsolutePath + "static").getPath() + File.separator;
    }

    /**
     * 得到build路径
     *
     * @return 结果
     */
    public static String getBuildPath() {

        String projectRootAbsolutePath = getProjectRootPath();

        int index = projectRootAbsolutePath.indexOf(UFOPConstants.FILE + UFOPConstants.COLON_IDENTIFIER);
        if (index != -1) {
            projectRootAbsolutePath = projectRootAbsolutePath.substring(0, index);
        }

        return new File(projectRootAbsolutePath + "build").getPath() + File.separator;
    }


    /**
     * 获取上传文件路径
     * 返回路径格式 “upload/yyyyMMdd/”
     *
     * @param identifier 文件名（一般传入md5或uuid,防止文件名重复）
     * @param extendName 文件扩展名
     * @return 返回上传文件路径
     */
    public static String getUploadFileUrl(String identifier, String extendName) {

        SimpleDateFormat formater = new SimpleDateFormat("yyyyMMdd");

        String path = ROOT_PATH + "/" + formater.format(new Date()) + "/";

        File dir = new File(UFOPUtils.getDataPath() + path);

        if (!dir.exists()) {
            dir.mkdirs();
        }

        path = path + identifier + "." + extendName;

        return path;
    }

    public static String getAliyunObjectNameByFileUrl(String fileUrl) {
        return getObjectName(fileUrl);
    }


    public static String formatPath(String path) {
        path = UFOPUtils.pathSplitFormat(path);
        if ("/".equals(path)) {
            return path;
        }
        if (path.endsWith("/")) {
            int length = path.length();
            return path.substring(0, length - 1);
        }
        return path;
    }

    public static String getTencentObjectNameByFileUrl(String fileUrl) {
        return getObjectName(fileUrl);
    }

    @NotNull
    private static String getObjectName(String fileUrl) {
        if (fileUrl.startsWith("/") || fileUrl.startsWith("\\")) {
            fileUrl = fileUrl.substring(1);
        }
        return fileUrl;
    }
}

